<?
require_once "inc.init.php";
define(GALLERY_PREFIX, "gallery_");

// THE OBJ:
$obj = new proyectos(); // <---- Change this !!!
$obj->setDbLink($dbLink);

// GETTING:
$obj->get( $_GET[$obj->_dbIndex] );
$isNew = $obj->getID()?FALSE:TRUE;

// MESSENGER
$messenger = new Messenger();

// SAVING:
switch($_GET['action'])
{
    case 'save_multiple_files':
            if ($user->isEnabledTo("write", $module->id))
            {
                $photoAlbum = new FileManagerPhotoAlbum($module->photoAttr);
                $photoAlbum->setDBLink($dbLink);
                if(!($obj->photo_albumID && $photoAlbum->get($obj->photo_albumID)))
                {
                    $photoAlbum->userID_modified=$user->getID();
                    $photoAlbum->create();
                    $obj->photo_albumID = $photoAlbum->getID();
                    $obj->userID_modified=$user->getID();
                    $obj->update();
                }
                $filesPost = $_FILES;
                if(is_array($filesPost[GALLERY_PREFIX.'multiple_file']))
                {
                    $files = array();
                    foreach($filesPost[GALLERY_PREFIX.'multiple_file'] as $attrName => $attr)
                    {
                        foreach($attr as $fileNo => $value)
                        {
                            if(!$filePost['error']) $file[$attrName] = $value;
                        }
                    }
                    $itemID = $photoAlbum->upload($file, $options);
                }
                $item = new FileManagerItemImage();
                $item->setDBLink($dbLink);
                $item->get($itemID);
                
                $response['files'][] = array("file_managerID" => $item->getID(),
                                             "name" => $item->file_name,
                                             "url"  => $item->getImageUrl(),
                                             "size" => 0,
                                             "thumbnailUrl" => $item->getIconUrl(),
                                             "deleteUrl" => $GLOBALS['CONF']['url']['root']."/modules/".$module->id."/ajax.php?action=remove_multiple_files&".$obj->_dbIndex."=".$obj->getID()."&file_managerID=".$item->getID(),
                                             "deleteType" => "delete");
                echo json_encode($response);
                exit();

            }
            else $messenger->addMessage($lang->translate("user_action_denied"));
        break;
    case 'get_multiple_files':
            if ($user->isEnabledTo("write", $module->id))
            {
                $photoAlbum = new FileManagerPhotoAlbum();
                $photoAlbum->setDBLink($dbLink);
                $photoAlbum->get($obj->photo_albumID);
                foreach($photoAlbum->_arFMItems as $item)
                {
                    $response['files'][] = array("file_managerID" => $item->getID(),
                                                 "name" => $item->file_name,
                                                 "url"  => $item->getImageUrl(),
                                                 "size" => 0,
                                                 "thumbnailUrl" => $item->getIconUrl(),
                                                 "deleteUrl" => $GLOBALS['CONF']['url']['root']."/modules/".$module->id."/ajax.php?action=remove_multiple_files&".$obj->_dbIndex."=".$obj->getID()."&file_managerID=".$item->getID(),
                                                 "deleteType" => "delete");
                }
                echo json_encode($response);
                exit();
            }
            else $messenger->addMessage($lang->translate("user_action_denied"));
        break;
    case 'remove_multiple_files':
            if ($user->isEnabledTo("write", $module->id))
            {
                $response["success"] = false;
                $response["file_manager_id"] = $_GET['file_managerID'];
                $photoAlbum = new FileManagerPhotoAlbum($module->photoAttr);
                $photoAlbum->setDBLink($dbLink);
                if($photoAlbum->get($obj->photo_albumID)){
                    if($photoAlbum->remove($_GET['file_managerID'])){
                        $response['success'] = true;
                    }
                }
                $query = mysql_query("SELECT itemID FROM file_manager_item WHERE file_managerID=".$obj->photo_albumID, $dbLink);
                $total_files = mysql_num_rows($query);
                $response["total_files"] = $total_files;
                $action = array("action"=>"delete","response"=>$response);
            }
            else $messenger->addMessage($lang->translate("user_action_denied"));
            echo json_encode($action);
            exit();
        break; 
    case 'save_order_multiple_files': 
            $messenger = new Messenger(); 
            if ($user->isEnabledTo("write", $module->id)) 
            { 
                $obj = new FileManagerItem(); 
                $table   = $obj->_dbTable; 
                $indexID = $obj->_dbIndex; 
                $newOrder = explode('&',$_POST['newOrderPhoto']); 
                if (is_array($newOrder)) 
                { 
                    for($i=0; $i<count($newOrder); $i++) 
                    { 
                        $orderedItemID = intval(str_replace('mySortable[]=', '', $newOrder[$i])); 
                        $sql = "UPDATE ".$table." SET `order`=". $i ." WHERE ".$indexID."=".$orderedItemID; 
                        $noErrors = mysql_query($sql); 
                        if (!$noErrors) break; 
                    } 
                    if ($noErrors) 
                    { 
                        $messenger->setMessageType(1); 
                        $messenger->addMessage("List of items ordered successfully."); 
                    } 
                    else $messenger->addMessage("Failed to sort the list of items:". mysql_error()); 
                } 
                $response["messages"] = $messenger->getArMessages(); 
                $response["success"] = $noErrors; 
                echo json_encode($response);
                exit();
            }
        break;
}
?>

